<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_tple.class.php');


require_once('tree_menu.class.php');


class Tree_Menu_HTML_Gatherer extends Tangra_Class {
	public static function get_html(I_TPLE $tple, $tpl_file, Tree_Menu $menu, $menu_id='nav_menu') {
		$items = $menu->get_items();

		if ($items) {
			$items_arr = array();

			foreach($items as $item) {
				$items_arr[] = Tree_Menu_HTML_Gatherer::get_item_arr($item);
			}

			$tple->assign('menu', $items_arr);
			$tple->assign('menu_id', $menu_id);
			$html = $tple->fetch($tpl_file);
		} else {
			$html = '';
		}

		return $html;
	}


	private static function get_item_arr(Tree_Menu_Item $item) {
		$ret['label'] = htmlentities($item->get_label());
		$ret['link'] = $item->get_link();
		$ret['target'] = $item->get_target();
		$ret['subitems'] = array();

		$subitems = $item->get_subitems();

		foreach($subitems as $s) {
			$ret['subitems'][] = Tree_Menu_HTML_Gatherer::get_item_arr($s);
		}

		return $ret;
	}
}